package com.lv.suyiyong.hfr.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.imageview.ShapeableImageView

import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.LoadSrcint
import com.lv.common.ktx.urlAddBorder
import com.lv.common.utils.Constants
import com.lv.common.utils.MoneyUtil
import com.lv.common.utils.YgxUtilsKt
import com.lv.common.widget.recyclerview.decoration.SpacesItemDecoration
import com.lv.service.bean.GoodsBean
import com.lv.service.bean.GoodsItemBean
import com.lv.suyiyong.hfr.R

/**
 * 作者： ygx
 * 创建日期：2023/4/3 9:04 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class GoodsPagerAdapter(
    private val context: Context, private val dataList: MutableList<GoodsBean>
) : PagerAdapter() {
    private val mBeans: MutableList<GoodsBean> = mutableListOf()

    init {
        mBeans.addAll(dataList)
    }

    fun setDatas(list: List<GoodsBean>) {
        mBeans.clear()
        mBeans.addAll(list)
        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return mBeans.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    @Suppress("UNREACHABLE_CODE")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_goods_vp, null)
//                val view = layoutInflater.inflate(R.layout.item_goods_vp,null)
        container.addView(view)
        val reContnet = view.findViewById<RecyclerView>(R.id.rv_content)
        val layoutManager: LinearLayoutManager =
            object : LinearLayoutManager(context) {
                //禁止竖向滑动 RecyclerView 为垂直状态（VERTICAL）
                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        reContnet.layoutManager = layoutManager
        val goodsBean = mBeans[position]
        val beans = mutableListOf<GoodsItemBean>()
        if (!goodsBean.items.isNullOrEmpty()) {
            beans.clear()
            beans.addAll(goodsBean.items!!)
        }
        val rvAdapter = object : BaseQuickAdapter<GoodsItemBean, BaseViewHolder>(
            R.layout.item_vp_goods, beans
        ) {
            override fun convert(holder: BaseViewHolder, item: GoodsItemBean) {
                val clParent = holder.getView<ConstraintLayout>(R.id.cl_parent)
                val tvName = holder.getView<TextView>(R.id.tv_name)
                val tvPrice = holder.getView<TextView>(R.id.tv_price)
                val ivImage = holder.getView<ShapeableImageView>(R.id.iv_image)
//                val url = item.image?.urlAddBorder(
//                    item.backdrop,
//                    SizeUtils.dp2px(101.0f), 80
//                )
                ivImage.LoadSrcint(item.image, com.lv.common.R.drawable.icon_logo_gray)
                tvName.text = item.name
                var price = item.price ?: 0.0f
                if (price == 0.0f) {
                    tvPrice.text = "面议"
                } else {
                    price = MoneyUtil.moneydiv("$price", "100")
                    tvPrice.text = YgxUtilsKt.create15PriceSpanTexts(price)
                }
                val layoutPosition = holder.layoutPosition
                clParent.setOnClickListener {
                    onViewClickListener?.onClick(layoutPosition, item)
//                    ARouter.getInstance().build(ARoutUtils.GoodsDetilsActivity)
//                        .withString("spuid", item.id)
//                        .navigation()
//                    (context as GoodsDetilsActivity).finish()
                }
            }
        }
        reContnet.adapter = rvAdapter
        rvAdapter.setEmptyView(getEmptyView(reContnet))
        reContnet.addItemDecoration(
            SpacesItemDecoration(
                SizeUtils.dp2px(7.0f),
                SizeUtils.dp2px(0.0f),
                false
            )
        )
        return view
    }

    fun getEmptyView(recyclerView: RecyclerView): View {
        val view: View = (context as Activity).layoutInflater.inflate(
            com.lv.common.R.layout.empty_center_view,
            recyclerView,
            false
        )
        val ivImg = view.findViewById<ImageView>(com.lv.common.R.id.iv_img)
        val tvEmpty = view.findViewById<TextView>(com.lv.common.R.id.tv_empty)
        ivImg.LoadSrc(Constants.EMPTY_URL)
        tvEmpty.text = "糟糕！没有其他商品啦~"
        return view
    }


    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    var onViewClickListener: OnPositionTListener<GoodsItemBean>? = null

}